﻿using System.Text;
using System.Linq.Expressions;

using Whoever.Framework.Enum;
using Whoever.Framework.Utils;

namespace System.Web.Mvc
{
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString KOLocalizedHelpFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, MessageType messageType, string visibleProperty)
        {
            var memberExpression = (MemberExpression)expression.Body;
            var entityName = memberExpression.Member.DeclaringType.Name;
            var propertyName = memberExpression.Member.Name;

            var resourceName = string.Format("Help.{0}.{1}", entityName, propertyName);

            var helpText = Localized.Text(resourceName);

            if (helpText == resourceName)
                helpText = Localized.Message("Empty", MessageType.Warning);

            var htmlBuilder = new StringBuilder();
            htmlBuilder.Append("<span data-bind=\"click: function(){ ui.HelpPopup(\'" + helpText + "\',");
            htmlBuilder.Append(((int)messageType).ToString());
            htmlBuilder.Append("); }");
            if (!string.IsNullOrEmpty(visibleProperty))
                htmlBuilder.Append(", visible: " + visibleProperty);
            switch (messageType)
            {
                case MessageType.Warning:
                    htmlBuilder.Append("\" class=\"glyphicon glyphicon-comment help-warning\" ");
                    break;
                case MessageType.Info:
                    htmlBuilder.Append("\" class=\"glyphicon glyphicon-comment help-info\" ");
                    break;
            }
            if (!string.IsNullOrEmpty(visibleProperty))
                htmlBuilder.Append("style=\"display: none;\"");
            htmlBuilder.Append("></span>");

            return new MvcHtmlString(htmlBuilder.ToString());
        }

        public static MvcHtmlString KOLocalizedHelp(this HtmlHelper htmlHelper, string name, MessageType messageType, string visibleProperty)
        {
            var helpText = Localized.Text(name);

            if (helpText == name)
                helpText = Localized.Message("Empty", MessageType.Warning);

            var htmlBuilder = new StringBuilder();
            htmlBuilder.Append("<span data-bind=\"click: function(){ ui.HelpPopup(\'" + helpText + "\',");
            htmlBuilder.Append(((int)messageType).ToString());
            htmlBuilder.Append("); }");
            if (!string.IsNullOrEmpty(visibleProperty))
                htmlBuilder.Append(", visible: " + visibleProperty);
            switch (messageType)
            {
                case MessageType.Warning:
                    htmlBuilder.Append("\" class=\"glyphicon glyphicon-comment help-warning\" ");
                    break;
                case MessageType.Info:
                    htmlBuilder.Append("\" class=\"glyphicon glyphicon-comment help-info\" ");
                    break;
            }
            if (!string.IsNullOrEmpty(visibleProperty))
                htmlBuilder.Append("style=\"display: none;\"");
            htmlBuilder.Append("></span>");

            return new MvcHtmlString(htmlBuilder.ToString());
        }

        public static MvcHtmlString KOSpanFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            var memberExpression = (MemberExpression)expression.Body;
            var propertyName = memberExpression.Member.Name;
            Func<TModel, TProperty> method = expression.Compile();
            TProperty value = method(htmlHelper.ViewData.Model);

            return new MvcHtmlString(string.Format("<span data-bind='text: {0}' class='pull-right'>{1}</span>", propertyName, value != null ? value.ToString() : string.Empty));
        }
    }
}